Ans. Spring is an open-source development framework for Enterprise Java. The core features of the Spring Framework can be used in developing any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring framework targets to make Java EE development easier to use and promote good programming practice by enabling a POJO-based programming model.
Ans.
Ans. The basic modules of the Spring framework are :
Ans. This is the basic Spring module, which provides the fundamental functionality of the Spring framework. BeanFactory is the heart of any spring-based application. Spring framework was built on the top of this module, which makes the Spring container.
Inclined to build a profession as Java Spring? Then here is the blog post on, explore "Java Spring Training"
Ans. A BeanFactory is an implementation of the factory pattern that applies Inversion of Control to separate the application’s configuration and dependencies from the actual application code.
The most commonly used BeanFactory implementation is the xmlBeanFactory class.
Ans. The most useful one is org.springframework.beans.factory.xml.xmlBeanFactory, which loads its beans based on the definitions contained in an XML file. This container reads the configuration metadata from an XML file and uses it to create a fully configured system or application.
Ans. The AOP module is used for developing aspects for our Spring-enabled application. Much of the support has been provided by the AOP Alliance in order to ensure the interoperability between Spring and other AOP frameworks. This module also introduces metadata programming to Spring.
Ans. With the JDBC abstraction and DAO module, we can be sure that we keep up the database code clean and simple, and prevent problems that result from a failure to close database resources. It provides a layer of meaningful exceptions on top of the error messages given by several database servers. It also makes use of Spring’s AOP module to provide transaction management services for objects in a Spring application.
Ans. Spring also supports for use of an object/relational mapping (ORM) tool over straight JDBC by providing the ORM module. Spring provides support to tie into several popular ORM frameworks, including Hibernate, JDO, and iBATIS SQL Maps. Spring’s transaction management supports each of these ORM frameworks as well as JDBC.
Ans. The Spring web module is built on the application context module, providing a context that is appropriate for web-based applications. This module also contains support for several web-oriented tasks such as transparently handling multipart requests for file uploads and programmatic binding of request parameters to your business objects. It also contains integration support with Jakarta Struts.
Ans. MVC framework is provided by Spring for building web applications. Spring can easily be integrated with other MVC frameworks, but Spring’s MVC framework is a better choice since it uses IoC to provide for a clean separation of controller logic from business objects. With Spring MVC you can declaratively bind request parameters to your business objects.
Ans. Spring configuration file is an XML file. This file contains the class information and describes how these classes are configured and introduced to each other.
Ans. The Spring IoC is responsible for creating the objects, managing them (with dependency injection (DI)), wiring them together, configuring them, as also managing their complete lifecycle.
Ans. IOC or dependency injection minimizes the amount of code in an application. It makes it easy to test applications since no singletons or JNDI lookup mechanisms are required in unit tests. Loose coupling is promoted with minimal effort and the least intrusive mechanism. IOC containers support eager instantiation and lazy loading of services.
Ans. The FileSystemXmlApplicationContext container loads the definitions of the beans from an XML file. The full path of the XML bean configuration file must be provided to the constructor. The ClassPathXmlApplicationContext container also loads the definitions of the beans from an XML file. Here, you need to set CLASSPATH properly because this container will look bean configuration XML file in CLASSPATH. The WebXmlApplicationContext: container loads the XML file with definitions of all beans from within a web application.
Ans. Application contexts provide a means for resolving text messages, a generic way to load file resources (such as images), they can publish events to beans that are registered as listeners. In addition, operations on the container or beans in the container, which have to be handled in a programmatic fashion with a bean factory, can be handled declaratively in an application context. The application context implements Messagesource, an interface used to obtain localized messages, with the actual implementation being pluggable.
Ans.
Ans. Dependency Injection, an aspect of Inversion of Control (IoC), is a general concept, and it can be expressed in many different ways.This concept says that you do not create your objects but describe how they should be created. You don’t directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (the IOC container) is then responsible for hooking it all up.
Ans.
Ans. You can use both Constructor-based and Setter-based Dependency Injection. The best solution is using constructor arguments for mandatory dependencies and setters for optional dependencies.
Ans. A Spring Bean definition contains all configuration metadata which is needed for the container to know how to create a bean, its lifecycle details and its dependencies.
Ans. There are three important methods to provide configuration metadata to the Spring Container:
Ans. When defining an in Spring, we can also declare a scope for the bean. It can be defined through the scope attribute in the bean definition. For example, when Spring has to produce a new bean instance each time one is needed, the bean’s scope attribute to be a prototype. On the other hand, when the same instance of a bean must be returned by Spring every time it is needed, the the bean scope attribute must be set to a singleton.
Ans. There are five scoped provided by the Spring Framework supports the following five scopes:
The default scope of a Spring Bean is Singleton.
Ans. Two important bean lifecycle methods. The first one is set up which is called when the bean is loaded into the container. The second method is the teardown method which is called when the bean is unloaded from the container. The bean tag has two important attributes (init-method and destroy-method) with which you can define your own custom initialization and destroy methods. There are also the corresponding annotations(@PostConstruct and @PreDestroy).
Ans. When a bean is only used as a property of another bean it can be declared as an inner bean. Spring’s XML-based configuration metadata provides the use of element inside the or elements of a bean definition, in order to define the so-called inner bean. Inner beans are always anonymous and they are always scoped as prototypes.
Ans. Spring offers the following types of collection configuration elements:
Ans. Wiring, or else bean wiring is the case when beans are combined together within the Spring container. When wiring beans, the Spring container needs to know what beans are needed and how the container should use dependency injection to tie them together.
Ans. The Spring container is able to autowire relationships between collaborating beans. This means that it is possible to automatically let Spring resolve collaborators (other beans) for a bean by inspecting the contents of the BeanFactory without using and elements.
Ans. The autowiring functionality has five modes which can be used to instruct Spring container to use autowiring for dependency injection:
Ans. Limitations of autowiring are:
Ans. Yes, you can.
Ans. Java-based configuration option enables you to write most of your Spring configuration without XML but with the help of few Java-based annotations. An example is a @Configuration annotation, which indicates that the class can be used by the Spring IoC container as a source of bean definitions. Another example is the@Beanannotated method that will return an object that should be registered as a bean in the Spring application context.
Ans. An alternative to XML setups is provided by annotation-based configuration which relies on the bytecode metadata for wiring up components instead of angle-bracket declarations. Instead of using XML to describe a bean wiring, the developer moves the configuration into the component class itself by using annotations on the relevant class, method, or field declaration.
Ans. Annotation wiring is not turned on in the Spring container by default. In order to use annotation-based wiring, we must enable it in our Spring configuration file by configuring element.
Ans. This annotation simply indicates that the affected bean property must be populated at configuration time, through an explicit property value in a bean definition or through autowiring. The container throws BeanInitializationException if the affected bean property has not been populated.
Ans. The @Autowired annotation provides more fine-grained control over where and how autowiring should be accomplished. It can be used to autowire bean on the setter method just like @Required annotation, on the constructor, on a property or pn methods with arbitrary names and/or multiple arguments.
Ans. When there are more than one beans of the same type and only one is needed to be wired with a property, the @Qualifier annotation is used along with @Autowired annotation to remove the confusion by specifying which exact bean will be wired.
Ans. When using the Spring JDBC framework the burden of resource management and error handling is reduced. So developers only need to write the statements and queries to get the data to and from the database. JDBC can be used more efficiently with the help of a template class provided by Spring framework, which is the JdbcTemplate.
Ans. JdbcTemplate class provides many convenience methods for doing things such as converting database data into primitives or objects, executing prepared and callable statements, and providing custom database error handling.
Ans. The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies like JDBC, Hibernate or JDO in a consistent way. This allows us to switch between the persistence technologies fairly easily and to code without worrying about catching exceptions that are specific to each technology.
Ans. Spring supports the following ORM’s:
Ans. Use Spring’s SessionFactory called LocalSessionFactory. The integration process is of 3 steps:
Ans. Spring supports two types of transaction management:
Ans.
Ans. Most users of the Spring Framework choose declarative transaction management because it is the option with the least impact on application code, and hence is most consistent with the ideals of a non-invasive lightweight container. Declarative transaction management is preferable over programmatic transaction management though it is less flexible than programmatic transaction management, which allows you to control transactions through your code.
Ans. Aspect-oriented programming, or AOP, is a programming technique that allows programmers to modularize crosscutting concerns or behavior that cuts across the typical divisions of responsibility, such as logging and transaction management.
Ans. The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules. It is a module that has a set of APIs providing cross-cutting requirements. For example, a logging module would be called the AOP aspect for logging. An application can have any number of aspects depending on the requirement. In Spring AOP, aspects are implemented using regular classes annotated with the @Aspect annotation (@AspectJ style).
Ans. The Concern is the behavior we want to have in a module of an application. A Concern may be defined as the functionality we want to implement. The cross-cutting concern is a concern that is applicable throughout the application and it affects the entire application. For example, logging, security, and data transfer are the concerns which are needed in almost every module of an application, hence they are cross-cutting concerns.
Ans. The join point represents a point in an application where we can plug-in an AOP aspect. It is the actual place in the application where an action will be taken using Spring AOP framework.
Ans. The advice is the actual action that will be taken either before or after the method execution. This is actual piece of code that is invoked during the program execution by the Spring AOP framework.
Spring aspects can work with five kinds of advice:
Ans. The pointcut is a set of one or more joinpoints where an advice should be executed. You can specify pointcuts using expressions or patterns.
Ans. An Introduction allows us to add new methods or attributes to existing classes.
Ans. A target object is an object being advised by one or more aspects. It will always be a proxy object. It is also referred to as the advised object.
Ans. A proxy is an object that is created after applying advice to a target object. When you think of client objects the target object and the proxy object are the same.
Ans.
BeanNameAutoProxyCreator
- DefaultAdvisorAutoProxyCreator
- Metadata autoproxying
Ans. Weaving is the process of linking aspects with other application types or objects to create an advised object. Weaving can be done at compile time, at load time, or at runtime.
Ans. In this implementation case, aspects are implemented using regular classes along with XML based configuration.
Ans. This implementation case (@AspectJ based implementation) refers to a style of declaring aspects as regular Java classes annotated with Java 5 annotations.
Ans. Spring comes with a full-featured MVC framework for building web applications. Although Spring can easily be integrated with other MVC frameworks, such as Struts, Spring’s MVC framework uses IoC to provide a clean separation of controller logic from business objects. It also allows to declaratively bind request parameters to business objects.
Ans. The Spring Web MVC framework is designed around a DispatcherServlet that handles all the HTTP requests and responses.
Ans. The WebApplicationContext is an extension of the plain ApplicationContext that has some extra features necessary for web applications. It differs from a normal ApplicationContextin that it is capable of resolving themes, and that it knows which servlet it is associated with.
Ans. Controllers provide access to the application behavior that you typically define through a service interface. Controllers interpret user input and transform it into a model that is represented to the user by the view. Spring implements a controller in a very abstract way, which enables you to create a wide variety of controllers.
Ans. The @Controller annotation indicates that a particular class serves the role of a controller. Spring does not require you to extend any controller base class or reference the Servlet API.
Ans. @RequestMapping annotation is used to map a URL to either an entire class or a particular handler method.
Ans. Spring boot is a module which used to simplify the use of the spring framework in Java development. It utilised to build stand-alone spring based applications which can simply run. So, it radically eliminates a lot of dependencies and configurations.
Ans. It allows avoiding complex configuration of XML which is there in spring
It carries an embedded Tomcat server.
It is effortless to maintain and creation of REST end-points
Deployment is effortless
Its architecture based on microservice.
Ans. Spring boot appeared into presence in October 2012, a user made a request asking spring framework and bootstrapping so that Jira can quickly start, and hence at the beginning of 2013 spring boot is made
In 2014 April spring boot version 1.0 is created.
On June 2014, version 1.1 created.
Version 1.2 came into existence in march 2015
In Dec 2016 version 1.3 is released
Version 1.4 came into existence in January of 2017
Spring boot version 1.5 created in 2017 February
Ans.
Spring | Springboot |
A framework of Web application which based on Java | A module which belongs to spring |
Spring is complicated than spring boot | Springboot is easy when compared to spring |
Takes an unopinionated aspect | Takes an opinionated aspect of a platform |
Gives libraries and tools to build optimised web applications |
It used to create a spring application which can simply execute |
Ans. It is simpler to understand and to develop spring applications.
Productivity increased
Reduces the time of development
Ans. To run the application individually and to decrease the development time.
To easily develop a production-ready spring application.
To avoid complicated XML configuration.
Offer a simpler way to start an application.
Ans. Spring boot started Actuator dependency
Spring boot started Security dependency
Spring boot started Web dependency
Spring boot starter Thyme leaf dependency
Spring boot starter Test dependency
Ans.
package com.howtodoinjava.exp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootexpApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootexpApplication.class, args);
}
}
Ans. From Oracle official site setup Java JDK
Download and setup Spring tool suit
New spring started project can be started by
Tick on File -> New -> Spring starter project
Fill the required details and include dependency and finish
Customise the application properties
Now the main file can be run as Java application.
Ans. Spring boot is a Java-based framework to generate microservices in order to create enterprise-level applications.
You frequently come over creating projects where you ought to connect to databases similar to MongoDB, etc. and save the actual password of DB connection inside that config file from spring boot project (application.yml or application.properties). Furthermore, tokens or passwords wanted for Authorisation to create another API call are additionally stored in a similar way.
You can truly avoid adding the original password inside the config file and utilise ‘jasypt-spring-boot‘, a java library.
Ans. Spring Boot lets creating production-ready applications immediately and gives non-functional characteristics: Embedded servers that are simple to deploy by some containers. It assists in observing the different components. It helps in configuring these components externally.
Ans. There are three main sources for external configuration in spring boot they are:
Application properties
Command-line properties
Profile-specific properties
Ans.
Spring boot version of 2.1.7 requires
Java 8+
The framework of 5.1.9+ of spring
Precise build support
Maven 3.3+
Gardel 4.4+
Support of servlet container
Servlet version 4.0 - Tomcat 9.0
Servlet version 3.1 - jetty 9.4
Servlet version 4.0 - undertow 2.0
Ans. By using server.http/2.enabled=true we can enable the support of HTTP/2 in spring boot
Ans. Microservice is a structure that permits software developers to develop services and deploy them independently; every service running possesses its own process, and this gains the lightweight model to maintain business applications.
Ans. Microservice can develop easily
It has effortless scalability
Configuration is minimum
Production time is very less.
Ans. An actuator is used to monitor and manage applications. An actuator is a tool that contains HTTP end-points while the application is driven to production, you can decide to manage and monitor your application utilizing HTTP end-points.
Ans. It is a server-side Java template locomotive for a web application. Its principal goal is to bring easy natural templates to your web application.
It can be combined with Spring Framework also perfect for HTML5 Java web applications.
Ans. Spring Boot dependency management is mainly used to manage dependencies and configuration automatically without you designating the version about that dependencies.
Ans. A custom end-point in spring boot 2.x can be created by using @enddpoint annotation, and Spring Boot also reveals end-points were utilising @WebEndpointor, @WebEndpointExtension above HTTP with the guidance of Spring MVC, Jersey, etc.
Ans. Follow the mention steps to deploy servers with spring boot.
Generating WAR from the project
Then, we can deploy this WAR file into your favorite server.
Ans. One of the main disadvantages of using spring boot is it is a bit overwhelming for new developers and time-consuming.
Ans. By removing the web dependencies from the classpath along with changing the way, we can create non-web applications.
Ans. The @Getmapping is a shortcut for @Requestmapping. These methods promote consumption. The consume choices are:
consumes= ‘plain/text’
consumes= {‘text/plain’, ‘*/application}
Ans. Spring Boot application scans whole beans plus package declarations when the application starts. You need to combine the @ComponentScan annotation for your class file to examine your components included in your project.
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.